/*
 * @(#)BooleanTypeHandle.java      0.1   2013-2-19
 *
 * Copyright (c) 2013 xQuant Co., Ltd. All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * xQuant Co., Ltd. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered
 * into with xQuant.
 */
package com.xquant.trade.utils;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Boolean类型与数据库类型转换器。
 * 将数据库字符1、0映射为boolean类型的true、false。
 * @version 0.1  2013-2-19
 * @author mohb
 *
 */
@MappedJdbcTypes(JdbcType.CHAR)
public class BooleanTypeHandle extends BaseTypeHandler<Boolean> {

	@Override
	public Boolean getNullableResult(ResultSet rs, String columnName)
			throws SQLException {
		String result = rs.getString(columnName);
		return "1".equals(result)?true:false;
	}

	@Override
	public Boolean getNullableResult(ResultSet rs, int columnIndex)
			throws SQLException {
		String result = rs.getString(columnIndex);
		return "1".equals(result)?true:false;
	}

	@Override
	public Boolean getNullableResult(CallableStatement rs, int columnIndex)
			throws SQLException {
		String result = rs.getString(columnIndex);
		return "1".equals(result)?true:false;
	}

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i,
			Boolean parameter, JdbcType arg3) throws SQLException {
		String value = (Boolean) parameter == true ? "1" : "0";
        ps.setString(i, value);

	}

}
